<?php
	class dathangdb
	{
		public function GetList($MaDatHang = 0, $MaNguoiDung = 0, $TinhTrang = -1, $order = '', $start = 0, $num = 30, $all = 0)
		{
			$sql =	"SELECT * ";
			$sql.=	"FROM tbl_giohang H, tbl_nguoidung N ";
			$sql.=	"WHERE H.MaNguoiDung = N.MaNguoiDung";
			
			if ($MaDatHang > 0)
				$sql .= " AND MaDatHang = " . dataservice::QuoteSmart($MaDatHang);
			
			if ($MaNguoiDung > 0)
				$sql .= " AND MaNguoiDung = " . dataservice::QuoteSmart($MaNguoiDung);
                
            if ($TinhTrang != -1)
				$sql .= " AND TinhTrang = " . dataservice::QuoteSmart($TinhTrang);
			
			if ($order != '')
				$sql .= " ORDER BY " . $order . " DESC";
			
			if($all == 0)
				$sql .= " LIMIT " . $start . ", ".$num;
			
			$db = new dataservice();
			$ret = $db->ExecuteQuery($sql);
			return $ret;
		}
		
		public function Add(&$madathang, $MaNguoiDung = 0, $DiaChi = '', $DienThoai = '', $NgayMuaHang = 0, $NgayGiaoHang = 0, $TinhTrang = 0)
		{
			$sql = "INSERT INTO tbl_giohang(MaNguoiDung, DiaChi, DienThoai, NgayMuaHang, NgayGiaoHang, TinhTrang) VALUES(" . dataservice::QuoteSmart($MaNguoiDung) . ", " . dataservice::QuoteSmart($DiaChi) . ", " . dataservice::QuoteSmart($DienThoai) . ", " . dataservice::QuoteSmart($NgayMuaHang) . ", " . dataservice::QuoteSmart($NgayGiaoHang) . ", " . dataservice::QuoteSmart($TinhTrang) . ")";
			
            //echo $sql;
			$db = new dataservice();
			$ret = $db->ExecuteNonQuery($sql);
			$madathang = mysql_insert_id();
			return $ret;
		}
        
        public function Update($MaGioHang = 0, $TinhTrang = 0)
        {
            $sql  = "UPDATE tbl_giohang ";
            $sql .= "SET TinhTrang = '$TinhTrang' " ;
            $sql .= "WHERE MaGioHang = '$MaGioHang'";
            $db = new dataservice();
			return $db->ExecuteNonQuery($sql);
        }
		
		public function Delete($MaGioHang = 0)
		{
			$sql = "DELETE FROM tbl_giohang WHERE (1)";
			
			if ($MaGioHang > 0)
				$sql .= " AND MaGioHang = " . dataservice::QuoteSmart($MaGioHang);
			
			$db = new dataservice();
			$ret = $db->ExecuteNonQuery($sql);
			return $ret;
		}
		
		public function CountList()
		{
			$sql = "SELECT COUNT(MaGioHang) AS SoLuongDatHang FROM tbl_giohang WHERE (1)";
			
			$db = new dataservice();
			$ret = $db->ExecuteQuery($sql);
			return $ret;
		}
        
        public function TotalPage($SoGioHang = 0, $MaGioHang = 0, $TinhTrang = -1)
        {
            $sql  = "SELECT * FROM tbl_giohang ";
            $sql .= "WHERE (1)";
            
            if($MaGioHang != 0)
                $sql .= " AND MaGioHang = " . dataservice::QuoteSmart($MaGioHang);
            
            if($TinhTrang != -1)
                $sql .= " AND TinhTrang = " . dataservice::QuoteSmart($TinhTrang);
                
            $data = new dataservice();
            $tongsodong = $data->ReturnNumRows($sql);
            $tongsotrang = ceil($tongsodong / $SoGioHang);
            return $tongsotrang;
        }
        
        public function Paging($SoGioHang = 1, $p = 1,  $MaGioHang = 0,  $TinhTrang = -1)
        {
            $x = ($p-1) * $SoGioHang;
            $sql  = "SELECT ND.HoTen, GH.MaGioHang, GH.DiaChi, GH.DienThoai, GH.NgayMuaHang, GH.NgayGiaoHang, GH.TinhTrang FROM tbl_giohang GH, tbl_nguoidung ND ";
            $sql .= "WHERE (1) AND GH.MaNguoiDung = ND.MaNguoiDung";
            
            if($MaGioHang != 0)
                $sql .= " AND MaGioHang = " . dataservice::QuoteSmart($MaGioHang);
                
            if($TinhTrang != -1)
                $sql .= " AND TinhTrang = " . dataservice::QuoteSmart($TinhTrang);                                
             
            $sql .= " limit $x,$SoGioHang";
            
            $data = new dataservice();
            return $data->ExecuteQuery($sql);
        }
	}
?>